package com.initial.controller;

import com.initial.entity.TbCrmAnnouncement;
import com.initial.entity.TbCrmDepartment;
import com.initial.service.TbCrmAnnouncementService;
import com.initial.service.impl.TbCrmDepartmentServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("manage/announcement")
public class AnnouncementController {
    @Resource
    private TbCrmAnnouncementService tbCrmAnnouncementService;
    @Resource
    private TbCrmDepartmentServiceImpl tbCrmDepartmentService;
    /**
     * 去公告页面
     * */
    @RequestMapping(value = "/tolist_announcement")
    public String tolistannouncement(){
        return "views/manage/announcement/list_announcement";
    }
    /**
     * 公告页  分页模糊 查询
     * */
    @RequestMapping("/findByPage.do")
    @ResponseBody
    public Map AnnouncementfindByPage(Integer page, Integer rows, String title, String departmentid){

        System.out.println(title);
        System.out.println(departmentid);

        Map<String,Object> map=new HashMap();
        List<TbCrmAnnouncement> tbCrmAnnouncements = tbCrmAnnouncementService.selectAllPage(page, rows, title, departmentid);
        map.put("result",tbCrmAnnouncements);;
        Integer integer = tbCrmAnnouncementService.selectAllCount(title, departmentid);
        map.put("totalRows",integer);
        Map<String,Object> map1=new HashMap();
        map1.put("data",map);
//        System.out.println(map);
        return map1;
    }
    /**
     * 去添加公告信息  页面
     * */
    @RequestMapping("/Announcement_add.do")
    public String toAnnouncementadd(Integer id,Model model){
        if (id != null){
            TbCrmAnnouncement tbCrmAnnouncement = tbCrmAnnouncementService.getById(id);
            model.addAttribute("tbCrmAnnouncement",tbCrmAnnouncement);
        }else {
            model.addAttribute("tbCrmAnnouncement",new TbCrmAnnouncement());
        }
//       查询部门的信息  部门下拉框
        List<TbCrmDepartment> list = tbCrmDepartmentService.list();
        model.addAttribute("tbCrmDepartment",list);
        return "views/manage/announcement/edit_announcement";
    }
    /**
     * 添加 公告信息   ***  修改   公告信息
     * */
    @RequestMapping("/Announcement_saveOrUpdate")
    @ResponseBody
    public Map AnnouncementsaveOrUpdate(TbCrmAnnouncement tbCrmAnnouncement){
        Map<String,Object> map=new HashMap();
        if (tbCrmAnnouncement.getId() != null){
            boolean b = tbCrmAnnouncementService.updateById(tbCrmAnnouncement);
            if (b){
                map.put("success",b);
                map.put("msg","修改成功");
            }else {
                map.put("success",b);
                map.put("msg","修改失败");
            }
        }else {
            /*页面要获取存在session中的用户ID*/
            if (tbCrmAnnouncement.getDepartment().equals("-1")){
                map.put("success",false);
                map.put("msg","请输入部门");
                return map;
            }
            boolean save = tbCrmAnnouncementService.save(tbCrmAnnouncement);
            if (save){
                map.put("success",save);
                map.put("msg","添加成功");
            }else {
                map.put("success",save);
                map.put("msg","添加失败");
            }
        }
        return map;
    }
    /**
     * 删除公告信息
     * */
    @RequestMapping("/Announcement_remove.do")
    @ResponseBody
    public Map AnnouncementRemove( String[] ids){
        Map map=new HashMap();
        Integer integer=null;
        for (String id : ids) {
            Integer i = Integer.parseInt(id);
            integer = tbCrmAnnouncementService.deleteById(i);
        }
        if (integer==1){
            map.put("success",true);
            map.put("msg","删除成功");
        }else {
            map.put("success",false);
            map.put("msg","删除失败");
        }
        return map;
    }
    /**
     * 查询部门的信息
     * 部门下拉框
     * */
/*    @RequestMapping("/Department_findAll.do")
    @ResponseBody
    public Map DepartmentFindAll(){
        Map map=new HashMap();
        List<TbCrmDepartment> list = tbCrmDepartmentService.list();
        map.put("data",list);
        return map;
    }*/
}
